package bb.lanxing.test;

import com.baidu.mapapi.model.LatLng;
import bb.lanxing.lib.kt.model.geo.IGeoPoint;
import bb.lanxing.util.CommonUtil;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.List;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;

public class PolylineUtil {
    public static double latlngE6ToNormal(int e6LatLng) {
        return e6LatLng / 1000000.0d;
    }

    public static void main(String[] args) {
        List<LatLng> decodePoints = CommonUtil.decodePoints("cba~DknodVg@Ss@yAs@QiA[_@SiBc@m@UoA[c@MmB[m@UmBo@w@S_BE]Km@Gi@CeBNK[HsBBcANoCGcAEmCAaACiCAgA?gCC_AMcBOAm@NAKDqA`@e@h@}A@m@IyBM{@m@aCI_AU_CRs@i@_BtA_AQsBTs@NeBB{@BkBCy@E{A?y@PuABUDk@Ao@GeBD[Ma@CSAmABw@?_BNOz@FTL\\OhEeEb@IP`@PNOPcBlAYb@c@b@o@?W}@UB|AsB^Sl@q@h@GZZp@LGz@sAfBi@P[ZQd@cAa@OcA");
        PrintStream printStream = System.out;
        printStream.println("latlng size : " + decodePoints.size() + " , 285");
        Iterator<LatLng> it2 = decodePoints.iterator();
        while (it2.hasNext()) {
            PrintStream printStream2 = System.out;
            printStream2.println("latlng : " + it2.next().toString());
        }
    }

    private static StringBuffer encodeSignedNumber(int num) {
        int i = num << 1;
        if (num < 0) {
            i = ~i;
        }
        return encodeNumber(i);
    }

    private static StringBuffer encodeNumber(int num) {
        StringBuffer stringBuffer = new StringBuffer();
        while (num >= 32) {
            stringBuffer.append((char) ((32 | (num & 31)) + 63));
            num >>= 5;
        }
        stringBuffer.append((char) (num + 63));
        return stringBuffer;
    }

    public static String encode(List<LatLng> polyline) {
        if (polyline == null || polyline.isEmpty()) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        int i = 0;
        int i2 = 0;
        for (LatLng latLng : polyline) {
            int i3 = (int) (latLng.latitude * 100000.0d);
            int i4 = (int) (latLng.longitude * 100000.0d);
            stringBuffer.append(encodeSignedNumber(i3 - i));
            stringBuffer.append(encodeSignedNumber(i4 - i2));
            i2 = i4;
            i = i3;
        }
        return stringBuffer.toString();
    }

    public static String encodeGeo(List<IGeoPoint> polyline) {
        if (polyline == null || polyline.isEmpty()) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int i = 0;
        int i2 = 0;
        for (IGeoPoint iGeoPoint : polyline) {
            int latitude = (int) (iGeoPoint.getLatitude() * 100000.0d);
            int longitude = (int) (iGeoPoint.getLongitude() * 100000.0d);
            sb.append(encodeSignedNumber(latitude - i));
            sb.append(encodeSignedNumber(longitude - i2));
            i2 = longitude;
            i = latitude;
        }
        return sb.toString();
    }

    public static BoundingBox makeBBox(List<IGeoPoint> points) {
        int size = points.size();
        int[][] iArr = (int[][]) Array.newInstance(int.class, size, 2);
        int i = Integer.MIN_VALUE;
        int i2 = Integer.MIN_VALUE;
        int i3 = Integer.MAX_VALUE;
        int i4 = Integer.MAX_VALUE;
        for (int i5 = 0; i5 < size; i5++) {
            GeoPoint geoPoint = new GeoPoint(points.get(i5).getLatitude(), points.get(i5).getLongitude());
            iArr[i5][0] = geoPoint.getLatitudeE6();
            iArr[i5][1] = geoPoint.getLongitudeE6();
            i3 = Math.min(i3, geoPoint.getLatitudeE6());
            i4 = Math.min(i4, geoPoint.getLongitudeE6());
            i = Math.max(i, geoPoint.getLatitudeE6());
            i2 = Math.max(i2, geoPoint.getLongitudeE6());
        }
        return new BoundingBox(i, i2, i3, i4);
    }
}
